/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 22, 2012  9:23:15 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.common
 * File Name : CostProjectUnitServices.java
 * TODO
 */
package jpa.service.promis.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import org.apache.log4j.Logger;

import promis.utility.CommonService;

import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.common.CostProject;
import jpa.entitys.promis.common.CostProjectUnit;
import jpa.entitys.promis.common.CostProjectUnitPK;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class CostProjectUnitServices extends JPAService {
	
	
	private static final Logger log = Logger.getLogger(CostProjectUnitServices.class);

	/**
	 * @param em
	 */
	public CostProjectUnitServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	

	public CostProjectUnit findById(CostProjectUnitPK id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "CostProjectUnit.findById"));				
	}
	
	public List<CostProject> findAll() throws Exception {
		
		return super.ConvertToYoureObject(super.findAll("CostProject.findAll"));
	}
	
	
	
	public CostProjectUnit InsertedOrUpdated(CostProjectUnit vlaue) throws Exception {
		CostProjectUnit obj = vlaue;
		try{		
			if(obj != null)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{			
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					super.inserted(obj);
				}else{			
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					super.updated(obj);
				}
			}			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<CostProjectUnit> Inserted(List<CostProjectUnit> vlauelist) throws Exception {
		try{
			if(!CommonService.IsNull(vlauelist)){
				List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
				for(CostProjectUnit obj : vlauelist)
				{
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}		
				
				if(CommonService.getSize(common_lst) > 0)
					super.inserted(common_lst);
			}
			
				
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<CostProjectUnit> Updated(List<CostProjectUnit> vlauelist) throws Exception {		
		
		try{
			if(!CommonService.IsNull(vlauelist))
			{
				List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
				for(CostProjectUnit obj : vlauelist)
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					
					common_lst.add(obj);
				}
				
				if(CommonService.getSize(common_lst) > 0)
					super.updated(common_lst);	
			}			
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}


}
